﻿using System;
using System.Collections.Generic;
using System.Text;
using PluginFramework;
using System.Windows.Forms;
using System.ComponentModel;

namespace SecondPlugin
{
    [Description("This is my Second plugin!")]
    public class SecondPlugin:IPlugin
    {
        private IApplication application = null;
        private String name = "";
        private String description = "";

        public IApplication Application
        {
            get
            {
                return application;
            }
            set
            {
                application = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public void Load()
        {
            if (application != null && application.BottomToolPanel != null)
            {
                ToolStripMenuItem item = application.MainMenuStrip.Items[0] as ToolStripMenuItem;
                ToolStripMenuItem itemTwo = new ToolStripMenuItem();
                itemTwo.Text = "插件测试";
                itemTwo.Name = "testTextBox";
                itemTwo.Click += new EventHandler(buttonClick);
                item.DropDownItems.Add(itemTwo);
            }
        }

        void buttonClick(object sender, EventArgs e)
        {
            MessageBox.Show("插件测试二");
        }

        //因为主要是为了演示的方便，我假设阅读这都没有移动插件添加的这个工具条到
        //其它的位置，比如停靠到左边，或者右边，如果改变了停靠的位置，这个工具条
        //将不能被卸载掉，因为我们只判断了底端的工具条容器
        public void UnLoad()
        {
            foreach (ToolStripMenuItem ts in application.MainMenuStrip.Items)
            {
                if (ts.Name == "testTextBox")
                {
                    application.MainMenuStrip.Items.Remove(ts);
                    break;
                }
                if(ts.DropDownItems.Count>0)
                {
                    foreach (ToolStripMenuItem it in ts.DropDownItems)
                    {
                        if(it.Name == "testTextBox")
                        {
                            ts.DropDownItems.Remove(it);
                            break;
                        }
                    }
                }
            }
        }

        public event EventHandler<EventArgs> Loading;
    }
}
